package org.lora.biz.ou;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.lora.biz.BizCommonConstant;
import org.lora.biz.ou.entity.Employee;
import org.lora.biz.ou.entity.EmployeeIdentityView;
import org.lora.biz.ou.entity.OrgLevel;
import org.lora.biz.ou.entity.Organization;
import org.lora.biz.ou.entity.Position;
import org.lora.biz.ou.service.IEmployeeIdentityService;
import org.lora.biz.ou.service.IOrganizationService;
import org.lora.core.annotation.Action;
import org.lora.core.annotation.AutoAssemble;
import org.lora.core.mybatis.Page;
import org.lora.exception.LoraCoreException;
import org.lora.mvc.annotation.RequestPath;
import org.lora.mvc.annotation.ResponseJSON;
import org.lora.mvc.entity.ViewData;
import org.lora.webframework.common.CommonConstant;
import org.lora.webframework.common.JsonResult;
import org.lora.webframework.common.WebframeBaseAction;
import org.lora.webframework.util.MD5Util;

import com.alibaba.fastjson.JSON;

/**
 * <p>
 * 类名:OrganizationManageAction
 * </p>
 * 描述:组织机构管理Action<br>
 * 创建时间:2016年8月31日 下午1:57:05<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年8月31日 Bladnir 创建<br>
 */
@Action
@RequestPath(path = "/biz/ou/org")
public class OrganizationManageAction extends WebframeBaseAction {

	@AutoAssemble
	private IOrganizationService organizationService;

	@AutoAssemble
	private IEmployeeIdentityService employeeIdentityService;

	/**
	 * 打开组织结构管理页面
	 * 
	 * @return
	 */
	@RequestPath(path = "/showOrganizationManage")
	public ViewData showOrganizationManage() {
		return new ViewData("/jsp/ou/organizationManage.jsp");
	}

	/**
	 * 打开组织机构级别管理页面
	 * 
	 * @return
	 */
	@RequestPath(path = "/showOrganizationLevelManage")
	public ViewData showOrganizationLevelManage() {
		return new ViewData("/jsp/ou/organizationLevelManage.jsp");
	}

	/**
	 * 打开添加人员到机构页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/showEmployeeIdentityAdd")
	public ViewData showEmployeeIdentityAdd(HttpServletRequest request) {

		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);
		String orgId = request.getParameter("orgId");
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put(LoraOUConstant.DOMAIN_ID_KEY, domainId);
		dataMap.put("orgId", orgId);

		return new ViewData("/jsp/ou/employeeIdentityAdd.jsp", dataMap);
	}

	/**
	 * 打开组织树形选择页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/showOrganizationTreeSelect")
	public ViewData showOrganizationTreeSelect(HttpServletRequest request) {
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put(LoraOUConstant.DOMAIN_ID_KEY, domainId);

		return new ViewData("/jsp/ou/organizationTreeSelect.jsp");
	}

	/**
	 * 打开添加机构页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/showOrganizationAdd")
	public ViewData showOrganizationAdd(HttpServletRequest request) {

		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);
		String parentId = request.getParameter("parentId");
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put(LoraOUConstant.DOMAIN_ID_KEY, domainId);
		dataMap.put("parentId", parentId);

		return new ViewData("/jsp/ou/organizationAdd.jsp", dataMap);
	}

	/**
	 * 保存员工信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/saveOrganization")
	@ResponseJSON
	public JsonResult saveOrganization(HttpServletRequest request) {
		String jsonData = request.getParameter("jsonData");
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);

		JsonResult result = new JsonResult();

		result.setFlag(CommonConstant.FAIL);

		try {
			// 封装数据
			Organization organization = JSON.parseObject(jsonData, Organization.class);

			// 验证数据
			if (validateOrganization(organization, domainId).equals(BizCommonConstant.VALIDATE_SUCCESS_STR)) {
				organizationService.saveOrganization(organization);
			}

			result.setFlag(CommonConstant.SUCCESS);

		} catch (Exception e) {
			result.setMsg(e.getMessage());
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	/**
	 * 获取组织机构树形数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/getOrganizationData")
	@ResponseJSON
	public Object getOrganizationData(HttpServletRequest request) {

		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);
		List<Organization> orgList = new ArrayList<>();
		try {
			orgList = organizationService.getOrgList(domainId, null, null);
		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return orgList;

	}

	/**
	 * 获取所有机构级别
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/getOrganizationLevelData")
	@ResponseJSON
	public JsonResult getOrganizationLevelData(HttpServletRequest request) {

		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);

		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);

		try {
			List<OrgLevel> orgLevelList = organizationService.getOrgLevelList(domainId, new HashMap<String, Object>(), null, null);
			result.setFlag(CommonConstant.SUCCESS);
			result.setData(orgLevelList);
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	/**
	 * 获取机构级别列表数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/getOrgLevelListData")
	@ResponseJSON
	public Object getOrgLevelListData(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();

		// 封装页面参数
		String[] parameterNameArray = new String[] { LoraOUConstant.DOMAIN_ID_KEY, "sortName", "sortOrder", "orgLevelName", "state",
				"orgLevel" };
		Map<String, Object> parameterMap = super.packageParameterMapFromRequest(request, parameterNameArray, true);

		// 设置分页Bean
		Page page = super.getPageBean(request);

		String domainId = getStringFromMap(LoraOUConstant.DOMAIN_ID_KEY, parameterMap);

		try {
			List<OrgLevel> list = organizationService.getOrgLevelList(domainId, parameterMap, page, null);
			int count = organizationService.getOrgLevelCount(domainId, parameterMap);
			resultMap.put("tableData", list);
			resultMap.put("count", count);
		} catch (Exception e) {
			businessLogger.error(e.getMessage(), e);
		}
		return resultMap;
	}

	/**
	 * @param organization
	 * @param domainId
	 * @return
	 */
	private String validateOrganization(Organization organization, String domainId) {
		// TODO 目前不验证
		return BizCommonConstant.VALIDATE_SUCCESS_STR;
	}

}
